package com.xdqzy.baseline.common.model;

import com.xdqzy.baseline.common.utils.ResourceUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author : zhangdongdong
 * @version V1.0
 * @Title : 接口响应结果包装
 * @ClassName: com.xdqzy.baseline.common.model.RestResult.java
 * @Description:
 * @date :  2020-10-20 17:49
 */
@Data
public class RestResult<T> {

    @ApiModelProperty("描述信息")
    private String msg;

    @ApiModelProperty(allowableValues = "0,1", value = "0-成功,1-失败")
    private Integer code;

    @ApiModelProperty("数据体")
    private T data;

    private RestResult(Integer code, String msg, T data) {
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    /**
     * Description: 构造
     *
     * @param code <br>
     * @param data <br>
     */
    public RestResult(Integer code, T data) {
        super();
        this.code = code;
        this.msg = ResourceUtil.getMessage(String.valueOf(code));
        this.data = data;
    }

    /**
     * Description: 构造
     *
     * @param code <br>
     * @param obj <br>
     */
    public RestResult(Integer code,  Object... obj) {
        super();
        this.code = code;
        this.msg = ResourceUtil.getMessage(String.valueOf(code), obj);
        this.data = (T) new Object();
    }

    public static <R> RestResult<R> buildSuccess(R data) {
        return new RestResult<>(0, "success", data);
    }

    public static <R> RestResult<R> buildSuccess() {
        return new RestResult<>(0, "success", null);
    }

    public static <R> RestResult<R> buildFail(Integer code, String msg) {
        return new RestResult<>(code, msg, null);
    }
}
